// 邮箱的正则校验
const validateEmail = (rule:any, value:any, callback:any) => {
  if(value){
    var email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if(!email.test(value)){
      callback(new Error('邮箱地址格式不正确'));
    }else{
      callback();
    }
  } else {
    //可以为空
    callback();
  }
};

// url地址的正则校验
const validateUrl = (rule:any, value:any, callback:any) => {
  if(value != ""){
    var Url = /^((ht|f)tps?):\/\/([\w-]+(\.[\w-]+)*\/?)+(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?$/;
    if(!Url.test(value)){
      callback(new Error('URL地址格式不正确'));
    }else{
      callback();
    }
  }
};

// 电话号码的正则校验
const validateTel = (rule:any, value:any, callback:any) => {
  if(value != ""){
    var tel = /^1[3456789]\d{9}$/;
    if(!tel.test(value)){
      callback(new Error('电话号码格式不正确'));
    }else{
      callback();
    }
  }
}

// 身份证号码的校验规则
const validateIdCard = (rule:any, value:any, callback:any) => {
  if(value != ""){
    var IdCard = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{7}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/;
    if(!IdCard.test(value)){
      callback(new Error('身份证号码格式不正确'));
    }else{
      callback();
    }
  }

}

// 日期校验规则
const validateDate = (rule:any, value:any, callback:any) => {
  if(value != ""){
    var Date = /^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/;
    if(!Date.test(value)){
      callback(new Error('日期格式不正确'));
    }else{
      callback();
    }
  }
}

// 数字校验
const validateNum = (rule:any, value:any, callback:any) => {
  if(!value || isNaN(value)){
    callback(new Error('数字格式不正确'));
  }else{
    callback();
  }
}

// 时间校验规则
const validateTime = (rule:any, value:any, callback:any) => {
  if(value!=null && value !=''){
    var flag = /^([0-1][0-9]|2[0-3]):([0-5][0-9])$/;
    if(!flag.test(value)){
      callback(new Error('时间格式不正确,格式为  HH:MM'));
    }else{
      callback();
    }
  }
}

// 自定义校验规则
const validateCustom = (val:any) =>{
  return (rule:any, value:any, callback:any) => {
    if(value!=null && value !=''){
      var flag = new RegExp(val.patternStr);
      if(!flag.test(value)){
        callback(new Error(String(val.errorMessage)));
      }else{
        callback();
      }
    }
  }
}

// 长度校验
const min = (val:any) =>{
  return (rule:any, value:any, callback:any) => {
    if(value!=null && value !=''){
      if(val.min && val.min > value.length){
        callback(new Error(`长度下限为 ${val.min} 个字符`));
      }else{
        callback();
      }
    } else {
       callback(new Error(`长度下限为 ${val.min} 个字符`));
    }
   }
}

const max = (val:any) =>{
  return (rule:any, value:any, callback:any) => {
    if(value!=null && value !=''){
      if(val.max && val.max < value.length){
        callback(new Error(`长度上限为 ${val.max} 个字符`));
      }else{
        callback();
      }
    } else {
       callback(new Error(`长度上限为 ${val.max} 个字符`));
    }
   }
}

// 接下来的校验规则都是业务常用的校验规则

// 整数位保留两位小数
export const numTofixed2 = (val:any) =>{
  return (rule:any, value:any, callback:any) => {
    if(value!=null && value !=''){
      var flag = new RegExp(val.patternStr);
      if(!flag.test(value)){
        callback(new Error(String(val.errorMessage)));
      }else{
        callback();
      }
    }
  }
}

export type MessageType = 'email' | 'url' | 'phone' | 'identity' | 'date' | 'number' | 'timeEmptyAble' | 'pattern'

export default {
  email: validateEmail,
  url: validateUrl,
  phone: validateTel,
  identity: validateIdCard,
  date: validateDate,
  number: validateNum,
  timeEmptyAble: validateTime,
  validateCustom: validateCustom,
  min: min,
  max: max,
  // 自定义校验规则
  pattern: ''
}
